package com.ymatou.iapi.productquery.compare;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.ymatou.iapi.productmongosync.service.MongocrudCall;
import com.ymatou.iapi.productquery.parameter.CatalogList.ProductInCartBean;
import com.ymatou.iapi.productquery.parameter.CatalogList.ProductInCartForDeliveryBean;
import com.ymatou.iapi.productquery.parameter.CatalogList.ProductPropertyBean;
import com.ymatou.iapi.productquery.parameter.ProductDescExtra.DescPropertyBean;
import com.ymatou.iapi.productquery.parameter.ProductDescExtra.ProductDescExtraBean;
import com.ymatou.iapi.productquery.parameter.ProductDetail.CatalogInDetailBean;
import com.ymatou.iapi.productquery.parameter.ProductDetail.CatalogPropsInDetailBean;
import com.ymatou.iapi.productquery.parameter.ProductDetail.ProductInDetailBean;
import com.ymatou.iapi.productquery.parameter.ProductHistory.ProductHistoryBean;
import com.ymatou.iapi.productquery.parameter.ProductList.ProductInListBean;
import com.ymatou.iapi.productquery.parameter.RecommendProd.RecommendProductBean;
import com.ymatou.iapi.productquery.parameter.SecKill.SecKillProductActivityStockBean;
import com.ymatou.iapi.productquery.parameter.TopProduct.TopProductInLiveBean;
import com.ymatou.iapi.productsync.service.MongocrudCallOld;
import com.ymttest.business.service.ProductQueryCallService;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;

public class SearchCompareHelper {
	public static MongocrudCall old_syc = new MongocrudCall();
	public static MongocrudCallOld new_syc = new MongocrudCallOld();
	
	
	
	
	public static <T> void compareIfDiffCallSycAndReCompare(String productId,T tgtBean,T actBean,String desc,FFunction<T,T,String,Boolean,Boolean> compareFunction){
		System.out.println(desc + ", productIds" + productId);
		boolean isfirstRunOK = compareFunction.apply(tgtBean, actBean, desc, true);
		if (isfirstRunOK){
			//do nothing
		}else{
			callSyc(productId);
			compareFunction.apply(tgtBean, actBean, desc, true);
		}
		
	}
	
	
	
	public static void callSyc(String productId){
		JSONObject oldRet = old_syc.callAndGetReturnData(0, productId, "AddProduct");
		JSONObject newRet = new_syc.callAndGetReturnData(0, productId, "AddProduct");
		System.out.println("调同步服务 productId"+productId);
		try {
			LoggerHelper.writeResult(LoggerHelper.syclogPath, "ProductId:"+productId+", 老同步返回:"+oldRet.getBoolean("IsSuccees")+",新同步返回:"+ newRet.getBoolean("success") ,true);
		} catch (JSONException e) {
			LoggerHelper.failure("调同步服务失败,productId"+productId, true);
		}
		YMTDateUtil.waitTime(3);
	}
	
	
	public static void callSyc_new(int activityId,String productId,String actionType){
		JSONObject newRet = new_syc.callAndGetReturnData(activityId, productId, actionType);
		System.out.println("调同步服务 productId"+productId);
		try {
			LoggerHelper.writeResult(LoggerHelper.syclogPath, "ProductId:"+productId+"activityId:"+activityId+",actionType:"+actionType+",新同步返回:"+ newRet.getBoolean("success") ,true);
		} catch (JSONException e) {
			LoggerHelper.failure("调同步服务失败,ProductId:"+productId+"activityId:"+activityId+",actionType:"+actionType, true);
		}
		YMTDateUtil.waitTime(3);
	}
	
	
	
	public static boolean compareProductInCartForDeliveryBean(ProductInCartForDeliveryBean tgtProductInCartForDeliveryBean,
			ProductInCartForDeliveryBean actProductInCartForDeliveryBean, String desc,boolean isWriteRet) {
		String desc1 = desc+", catalogId: "+ tgtProductInCartForDeliveryBean.getCatalogId();
		
		boolean isRight = true;
		
		isRight = isRight &&  compareProductInCartBean(tgtProductInCartForDeliveryBean,actProductInCartForDeliveryBean,desc1,isWriteRet);
		
		isRight = isRight && LoggerHelper.verifyEquals(tgtProductInCartForDeliveryBean.getExtraDeliveryFee(),actProductInCartForDeliveryBean.getExtraDeliveryFee(),desc1,isWriteRet);
		isRight = isRight && LoggerHelper.verifyEquals(tgtProductInCartForDeliveryBean.getExtraDeliveryType(),actProductInCartForDeliveryBean.getExtraDeliveryType(),desc1,isWriteRet);
		
		return isRight;
	}
	
	// 批量检查
	public static boolean compareProductInCartBean(List<String> catalogIdList,List<ProductInCartBean> tgtProductInCartBeanList, List<ProductInCartBean> actProductInCartBeanList,
			String apiName,boolean isWriteRet) {
		boolean isRight = true;
		
		isRight = isRight &&  LoggerHelper.verifyEquals(tgtProductInCartBeanList.size(), actProductInCartBeanList.size(), apiName +",期待结果数量应该相同",isWriteRet);

		
		
		for(int i=0;i<catalogIdList.size();i++){
			 String ele = catalogIdList.get(i);
			 ProductInCartBean tgtProductInCartBean = tgtProductInCartBeanList.stream()
						.filter(tgt -> tgt.getCatalogId().equals(ele)).findAny().orElse(null);
				ProductInCartBean actProductInCartBean = actProductInCartBeanList.stream()
						.filter(act -> act.getCatalogId().equals(ele)).findAny().orElse(null);
				if (tgtProductInCartBean == null && actProductInCartBean ==null){
					//do nothing
				}else{
					isRight = isRight && LoggerHelper.verifyNotNull(tgtProductInCartBean, apiName+"期待结果不该为null catalogId:" + ele,isWriteRet);
					isRight = isRight && LoggerHelper.verifyNotNull(actProductInCartBean, apiName+"实际结果不该为null catalogId:" + ele,isWriteRet);
					isRight = isRight && compareProductInCartBean(tgtProductInCartBean, actProductInCartBean, apiName,isWriteRet);
				}
		}
		
		return isRight;

	}

	public static boolean compareProductInCartBean(ProductInCartBean tgtProductInCartBean,
			ProductInCartBean actProductInCartBean, String desc,boolean isWriteRet) {
		
		boolean isRight = true;
		
		
		// 第一层数据对比
		Map<String, Object> tgtMap = MapUtil.pojoToMap(tgtProductInCartBean);
		tgtMap.remove("Properties");
		tgtMap.remove("LiveProduct");
		tgtMap.remove("ProductActivity");
		
		
		
		Map<String, Object> actMap = MapUtil.pojoToMap(actProductInCartBean);
		actMap.remove("Properties");
		actMap.remove("LiveProduct");
		actMap.remove("ProductActivity");
		

		
		if (Double.parseDouble(actProductInCartBean.getVersion())>Double.parseDouble(tgtProductInCartBean.getVersion())){
			tgtMap.remove("Version");
			actMap.remove("Version");
		}
		
		//removeTmp
		ProductQueryCallService.removeIgnoreKey(tgtMap);
		ProductQueryCallService.removeIgnoreKey(actMap);
		
		
		

		// 第二层数据对比
		// 直播
		if (tgtProductInCartBean.getLiveProduct() != null) {
			Map<String, Object> tgtLiveMap = MapUtil.pojoToMap(tgtProductInCartBean.getLiveProduct());
			Map<String, Object> actLiveMap = MapUtil.pojoToMap(actProductInCartBean.getLiveProduct());
			
			//直播中品类id和品类名 去除比对
			ProductQueryCallService.removeLiveProdIgnoreKey(tgtLiveMap);
			ProductQueryCallService.removeLiveProdIgnoreKey(actLiveMap);
			
			ProductQueryCallService.removeIgnoreKey(tgtLiveMap);
			ProductQueryCallService.removeIgnoreKey(actLiveMap);
			
			isRight = isRight &&  AssertServiceHelper.assertResultEqual(tgtLiveMap, actLiveMap, desc,isWriteRet);
		} else {
			
			if (actProductInCartBean.getLiveProduct()!=null){
				//如果当前直播在五分钟内，排除比对
				long newTime = YMTDateUtil.getDate().getTime();
				boolean startNearNow = Math.abs(actProductInCartBean.getLiveProduct().getStartTime().getTime() - newTime) <= 5 * 60 * 1000;
				boolean endNearNow = Math.abs(actProductInCartBean.getLiveProduct().getEndTime().getTime() - newTime) <= 5 * 60 * 1000;
				
				if(!startNearNow && !endNearNow){
					isRight = isRight &&  LoggerHelper.verifyEquals(false, true, desc+",LiveProduct 应该为null, livdId:"+ actProductInCartBean.getLiveProduct().getLiveId(),isWriteRet);
				}else{
					tgtMap.remove("ValidStart");
					tgtMap.remove("ValidEnd");
				}
			}
		}
		
		
		// 活动
		if (tgtProductInCartBean.getProductActivity() != null) {
			Map<String, Object> tgtActivityMap = MapUtil.pojoToMap(tgtProductInCartBean.getProductActivity());
			tgtActivityMap.remove("ActivityCatalogList");
			Map<String, Object> actActivityMap = MapUtil.pojoToMap(actProductInCartBean.getProductActivity());
			actActivityMap.remove("ActivityCatalogList");

			ProductQueryCallService.removeActivityIgnoreKey(tgtActivityMap);
			ProductQueryCallService.removeActivityIgnoreKey(actActivityMap);
			
			
			isRight = isRight &&  AssertServiceHelper.assertResultEqual(tgtActivityMap, actActivityMap, desc,isWriteRet);

			isRight = isRight &&  AssertServiceHelper.checkArray(tgtProductInCartBean.getProductActivity().getActivityCatalogList(),
					actProductInCartBean.getProductActivity().getActivityCatalogList(),desc,isWriteRet);

		} else {
			if (actProductInCartBean.getProductActivity()!=null){
				isRight = isRight &&  LoggerHelper.verifyEquals(false, true, desc+",ProductActivity 应该为null, inaid:"+ actProductInCartBean.getProductActivity().getProductInActivityId(),isWriteRet);
			}
			isRight = isRight &&  LoggerHelper.verifyIsNull(actProductInCartBean.getProductActivity(),  desc+",ProductActivity 应该为null",isWriteRet);
		}

		// 属性比对 todo
		List<ProductPropertyBean> tgtProps = tgtProductInCartBean.getProperties();
		List<ProductPropertyBean> actProps = actProductInCartBean.getProperties();
		if ( actProps==null){
			isRight = isRight &&LoggerHelper.verifyEquals(true,false,desc+",期待属性节点 Properties 属性为null",isWriteRet);
		}else{
			isRight = isRight &&  LoggerHelper.verifyEquals(tgtProps.size(), actProps.size(), desc+",属性值个数比较",isWriteRet);
			if (tgtProps.size() == 1 && tgtProps.get(0).getPropertyName() == null) {
				// 单规格
				isRight = isRight &&  LoggerHelper.verifyEquals(1, actProps.size(), desc+",单规格，属性节点个数为1",isWriteRet);
				isRight = isRight &&  LoggerHelper.verifyIsNull(actProps.get(0).getPropertyName(), desc+",单规格属性为null",isWriteRet);
				isRight = isRight &&  LoggerHelper.verifyIsNull(actProps.get(0).getPropertyPictureUrl(), desc+",单规格属性为null",isWriteRet);
				isRight = isRight &&  LoggerHelper.verifyIsNull(actProps.get(0).getPropertyValue(), desc+",单规格属性为null",isWriteRet);

			} else {
				
				for(int k=0;k<tgtProps.size();k++){
					ProductPropertyBean ele = tgtProps.get(k);
					ProductPropertyBean actProp = actProps.stream()
							.filter(y -> y.getPropertyName().equals(ele.getPropertyName())).findFirst().orElse(null);
					Map<String, Object> tgtPropMap = MapUtil.pojoToMap(ele);
					Map<String, Object> actPropMap = MapUtil.pojoToMap(actProp);
					isRight = isRight &&  AssertServiceHelper.assertResultEqual(tgtPropMap, actPropMap, desc,isWriteRet);
				}
				
			}
		}
		
		ProductQueryCallService.handlerVaildStartAndEndTime(tgtProductInCartBean.getProductId(),tgtProductInCartBean.getSellerId(),tgtMap,actMap);
		isRight = isRight &&  AssertServiceHelper.assertResultEqual(tgtMap, actMap, desc,isWriteRet);
		
		
		return isRight;
	}

	
	
	// 批量检查
	public static boolean compareProductInDetailBean(List<String> productList,List<ProductInDetailBean> tgtProductInDetailBeanList, List<ProductInDetailBean> actProductInDetailBeanList,
			String apiName,boolean isWriteRet) {
		
		boolean isRight = true;
		
		isRight = isRight && LoggerHelper.verifyEquals(tgtProductInDetailBeanList.size(), actProductInDetailBeanList.size(), apiName +",期待结果数量应该相同",isWriteRet);

		
		for(int i=0;i<productList.size();i++){
			String ele = productList.get(i);
			ProductInDetailBean tgtProductInDetailBean = tgtProductInDetailBeanList.stream()
					.filter(tgt -> tgt.getProductId().equals(ele)).findAny().orElse(null);
			ProductInDetailBean actProductInDetailBean = actProductInDetailBeanList.stream()
					.filter(act -> act.getProductId().equals(ele)).findAny().orElse(null);
			
			if (tgtProductInDetailBean == null &&actProductInDetailBean ==null ){
				//do nothing
			}else{
				isRight = isRight &&LoggerHelper.verifyNotNull(tgtProductInDetailBean, apiName+"期待结果不该为null productId:" + ele,isWriteRet);
				isRight = isRight &&LoggerHelper.verifyNotNull(actProductInDetailBean, apiName+"实际结果不该为null productId:" + ele,isWriteRet);
	
				isRight = isRight && compareProductInDetailBean(tgtProductInDetailBean, actProductInDetailBean, apiName,isWriteRet);
			}
		}
		
		
		return isRight;

	}
	
	public static boolean compareProductInDetailBean(ProductInDetailBean tgtProductInDetailBean,ProductInDetailBean actProductInDetailBean,String desc,boolean isWriteRet){
		
		
		boolean isRight = true;
		 
		
		if (actProductInDetailBean.getSizePicList()!=null && actProductInDetailBean.getSizePicList().size()==0){
			actProductInDetailBean.setSizePicList(null);
		}
		
		if (tgtProductInDetailBean.getSizePicList()!=null && tgtProductInDetailBean.getSizePicList().size()==0){
			tgtProductInDetailBean.setSizePicList(null);
		}
		
		//第一层数据对比
		Map<String, Object> tgtMap = MapUtil.pojoToMap(tgtProductInDetailBean);
		tgtMap.remove("ProductActivity");
		tgtMap.remove("NextActivity");
		tgtMap.remove("LiveProduct");
		tgtMap.remove("CatalogList");
		
		
		Map<String, Object> actMap = MapUtil.pojoToMap(actProductInDetailBean);
		actMap.remove("ProductActivity");
		actMap.remove("NextActivity");
		actMap.remove("LiveProduct");
		actMap.remove("CatalogList");
		
		
		
		
		if (Double.parseDouble(actProductInDetailBean.getVersion())>Double.parseDouble(tgtProductInDetailBean.getVersion())){
			tgtMap.remove("Version");
			actMap.remove("Version");
		}
		
		
		

		//如果isnew=true 比较新品时间
		if (tgtProductInDetailBean.isIsNewProduct()|| actProductInDetailBean.isIsNewProduct()){
			//60秒的容错
			boolean startEqual = Math.abs(tgtProductInDetailBean.getNewStartTime().getTime() - actProductInDetailBean.getNewStartTime().getTime()) <= 60 * 1000;
			boolean endEqual = Math.abs(tgtProductInDetailBean.getNewEndTime().getTime() - actProductInDetailBean.getNewEndTime().getTime()) <= 60 * 1000;
			
			//如果有一个时间不对，差值超过60秒。和本地时间比较
			if (!startEqual || !endEqual){
				long newTime = YMTDateUtil.getDate().getTime();
				boolean startNearNow = Math.abs(actProductInDetailBean.getNewStartTime().getTime() - newTime) <= 5 * 60 * 1000;
				boolean endNearNow = Math.abs(actProductInDetailBean.getNewEndTime().getTime() - newTime) <= 5 * 60 * 1000;
				
				//只要有一个时间接近当前时间，就认为ok
				if(!startNearNow && !endNearNow){
					isRight = isRight &&LoggerHelper.verifyEquals(true, false, desc + ", key:[NewStartTime],target value:[" + tgtProductInDetailBean.getNewStartTime() + "],actual value:[" + actProductInDetailBean.getNewStartTime()+"]",isWriteRet);
					isRight = isRight &&LoggerHelper.verifyEquals(true, false, desc + ", key:[NewStartTime],target value:[" + tgtProductInDetailBean.getNewEndTime() + "],actual value:[" + actProductInDetailBean.getNewEndTime()+"]",isWriteRet);
				}
			}
		}
		
		//removeTmp
		ProductQueryCallService.removeIgnoreKey(tgtMap);
		ProductQueryCallService.removeIgnoreKey(actMap);
			
		
		
		
		
		//第二层数据比对
		//规格
		List<CatalogInDetailBean> tgtCatalogInDetailBeanList = tgtProductInDetailBean.getCatalogList();
		List<CatalogInDetailBean> actCatalogInDetailBeanList = actProductInDetailBean.getCatalogList();
		LoggerHelper.verifyEquals(tgtCatalogInDetailBeanList.size(), actCatalogInDetailBeanList.size(), desc+",规格数量应该相等",isWriteRet);
		
		
		for(CatalogInDetailBean tgt:tgtCatalogInDetailBeanList){
			CatalogInDetailBean actCatalogInDetailBean = actCatalogInDetailBeanList.stream().filter(act->act.getCatalogId().equals(tgt.getCatalogId())).findFirst().orElse(null);
			if (actCatalogInDetailBean!=null){
				Map<String, Object> tgtCatalogMap = MapUtil.pojoToMap(tgt);
				tgtCatalogMap.remove("PropertyList");
				Map<String, Object> actCatalogMap = MapUtil.pojoToMap(actCatalogInDetailBean);
				actCatalogMap.remove("actCatalogMap");
				
				ProductQueryCallService.removeIgnoreKey(tgtCatalogMap);
				ProductQueryCallService.removeIgnoreKey(actCatalogMap);
				
				isRight = isRight &&AssertServiceHelper.assertResultEqual(tgtCatalogMap,actCatalogMap,desc,isWriteRet);
				
				//属性 值比较
				List<CatalogPropsInDetailBean> tgtProps = tgt.getPropertyList();
				List<CatalogPropsInDetailBean> actProps = actCatalogInDetailBean.getPropertyList();
				
				isRight = isRight &&LoggerHelper.verifyEquals(tgtProps.size(), actProps.size(), desc+", catalogId:"+tgt.getCatalogId()+" 属性值个数不匹配",isWriteRet);
				
				if (tgtProps.size()==1 && tgtProps.get(0).getName()==null){
					//单规格
					if (actProps.size()==0){
						isRight = isRight &&LoggerHelper.verifyEquals(true,false,desc+", 单规格property应该有节点",isWriteRet);
					}else{
						isRight = isRight &&LoggerHelper.verifyIsNull(actProps.get(0).getName(), desc+",单规格属性为null",isWriteRet);
						isRight = isRight &&LoggerHelper.verifyIsNull(actProps.get(0).getPicUrl(), desc+",单规格属性为null",isWriteRet);
						isRight = isRight &&LoggerHelper.verifyIsNull(actProps.get(0).getValue(), desc+",单规格属性为null",isWriteRet);
					}
					
				}else{
					for(CatalogPropsInDetailBean ele:tgtProps){
						CatalogPropsInDetailBean actProp = actProps.stream().filter(y->y.getName().equals(ele.getName())).findFirst().orElse(null);
						Map<String, Object> tgtPropMap = MapUtil.pojoToMap(ele);
						Map<String, Object> actPropMap = MapUtil.pojoToMap(actProp);
						isRight = isRight &&AssertServiceHelper.assertResultEqual(tgtPropMap,actPropMap,desc,isWriteRet);
					}
				}
			}else{
				isRight = isRight &&LoggerHelper.verifyEquals(false, true, desc+", catalogId: "+tgt.getCatalogId()+"中不存在",isWriteRet);
			}
		}
			
		
		//直播
		if (tgtProductInDetailBean.getLiveProduct()!= null){
			Map<String, Object> tgtLiveMap = MapUtil.pojoToMap(tgtProductInDetailBean.getLiveProduct());
			Map<String, Object> actLiveMap = MapUtil.pojoToMap(actProductInDetailBean.getLiveProduct());
			
			ProductQueryCallService.removeLiveProdIgnoreKey(tgtLiveMap);
			ProductQueryCallService.removeLiveProdIgnoreKey(actLiveMap);
			
			ProductQueryCallService.removeIgnoreKey(tgtLiveMap);
			ProductQueryCallService.removeIgnoreKey(actLiveMap);
			
			isRight = isRight &&AssertServiceHelper.assertResultEqual(tgtLiveMap,actLiveMap,desc,isWriteRet);
		}else{
			if (actProductInDetailBean.getLiveProduct()!=null){
				//如果当前直播在五分钟内，排除比对
				long newTime = YMTDateUtil.getDate().getTime();
				boolean startNearNow = Math.abs(actProductInDetailBean.getLiveProduct().getStartTime().getTime() - newTime) <= 5 * 60 * 1000;
				boolean endNearNow = Math.abs(actProductInDetailBean.getLiveProduct().getEndTime().getTime() - newTime) <= 5 * 60 * 1000;
				
				if(!startNearNow && !endNearNow){
					isRight = isRight &&LoggerHelper.verifyEquals(false, true, desc+",LiveProduct 应该为null, livdId:"+ actProductInDetailBean.getLiveProduct().getLiveId(),isWriteRet);
				}else{
					tgtMap.remove("ValidStart");
					tgtMap.remove("ValidEnd");
				}
			}
		}
		
		//当前活动
		if (tgtProductInDetailBean.getProductActivity()!= null){
			Map<String, Object> tgtActivityMap = MapUtil.pojoToMap(tgtProductInDetailBean.getProductActivity());
			tgtActivityMap.remove("CatalogList");
			Map<String, Object> actActivityMap = MapUtil.pojoToMap(actProductInDetailBean.getProductActivity());
			actActivityMap.remove("CatalogList");
			
			ProductQueryCallService.removeActivityIgnoreKey(tgtActivityMap);
			ProductQueryCallService.removeActivityIgnoreKey(actActivityMap);


			
			isRight = isRight &&AssertServiceHelper.assertResultEqual(tgtActivityMap,actActivityMap,desc,isWriteRet);
			
			isRight = isRight &&AssertServiceHelper.checkArray(tgtProductInDetailBean.getProductActivity().getCatalogList(),
					actProductInDetailBean.getProductActivity().getCatalogList(),desc,isWriteRet);
		}else{
			if (actProductInDetailBean.getProductActivity()!=null){
				isRight = isRight &&LoggerHelper.verifyEquals(false, true, desc+",ProductActivity 应该为null, inaid:"+ actProductInDetailBean.getProductActivity().getProductInActivityId(),isWriteRet);
			}
			
			
			isRight = isRight &&LoggerHelper.verifyIsNull(actProductInDetailBean.getProductActivity(), desc+",ProductActivity 应该为null",isWriteRet);
		}
		
		//下一场活动
		if (tgtProductInDetailBean.getNextActivity()!= null){
			Map<String, Object> tgtActivityMap = MapUtil.pojoToMap(tgtProductInDetailBean.getNextActivity());
			tgtActivityMap.remove("CatalogList");
			Map<String, Object> actActivityMap = MapUtil.pojoToMap(actProductInDetailBean.getNextActivity());
			actActivityMap.remove("CatalogList");
			
			ProductQueryCallService.removeActivityIgnoreKey(tgtActivityMap);
			ProductQueryCallService.removeActivityIgnoreKey(actActivityMap);
			
			isRight = isRight &&AssertServiceHelper.assertResultEqual(tgtActivityMap,actActivityMap,desc,isWriteRet);
			
			isRight = isRight &&AssertServiceHelper.checkArray(tgtProductInDetailBean.getNextActivity().getCatalogList(),
					actProductInDetailBean.getNextActivity().getCatalogList(),desc,isWriteRet);
		}else{
			if (actProductInDetailBean.getNextActivity()!=null){
				isRight = isRight &&LoggerHelper.verifyEquals(false, true, desc+",ProductActivity 应该为null, inaid:"+ actProductInDetailBean.getNextActivity().getProductInActivityId(),isWriteRet);
			}
			
			isRight = isRight &&LoggerHelper.verifyIsNull(actProductInDetailBean.getNextActivity(), desc+",NextActivity 应该为null",isWriteRet);
		}
		
		
		
		
		ProductQueryCallService.handlerVaildStartAndEndTime(tgtProductInDetailBean.getProductId(),tgtProductInDetailBean.getSellerId(),tgtMap,actMap);
		isRight = isRight &&AssertServiceHelper.assertResultEqual(tgtMap,actMap,desc,isWriteRet);
		
		
		return isRight;
		
	}
	
	// 批量检查
	public static boolean compareProductHistoryBean(List<String> productList,List<ProductHistoryBean> tgtProductHistoryBeanList,List<ProductHistoryBean> actProductHistoryBeanList
			,String apiName,boolean isWriteRet){
		
		boolean isRight = true;
		
		isRight = isRight &&LoggerHelper.verifyEquals(tgtProductHistoryBeanList.size(), actProductHistoryBeanList.size(), apiName +",期待结果数量应该相同",isWriteRet);

		for(String ele:productList){
			ProductHistoryBean tgtProductHistoryBean = tgtProductHistoryBeanList.stream()
					.filter(tgt -> tgt.getProductId().equals(ele)).findAny().orElse(null);
			ProductHistoryBean actProductHistoryBean = actProductHistoryBeanList.stream()
					.filter(act -> act.getProductId().equals(ele)).findAny().orElse(null);
			if (tgtProductHistoryBean==null && actProductHistoryBean==null){
				//do nothing 新老接口都查不到
			}else{
				isRight = isRight &&LoggerHelper.verifyNotNull(tgtProductHistoryBean, apiName+"期待结果不该为null productId:" + ele,isWriteRet);
				isRight = isRight &&LoggerHelper.verifyNotNull(actProductHistoryBean, apiName+"实际结果不该为null productId:" + ele,isWriteRet);
				isRight = isRight && compareProductHistoryBean(tgtProductHistoryBean, actProductHistoryBean, apiName + ", productId: "+ tgtProductHistoryBean.getProductId(),isWriteRet);
			}
		}

			
		
		return isRight;
	}	
	
	public static boolean compareProductHistoryBean(ProductHistoryBean tgtProductHistoryBean,ProductHistoryBean actProductHistoryBean,String desc,boolean isWriteRet){
		Map<String, Object> tgtMap = MapUtil.pojoToMap(tgtProductHistoryBean);
		Map<String, Object> actMap = MapUtil.pojoToMap(actProductHistoryBean);
		return AssertServiceHelper.assertResultEqual(tgtMap,actMap,desc,isWriteRet);
	} 
	
	
	
	// 批量检查
	
	public static List<ProductReturnBean> compareRecommendProductBean(List<RecommendProductBean> tgtRecommendProductBeanList,List<RecommendProductBean> actRecommendProductBeanList,String apiName,boolean isWriteRet){
		
		List<ProductReturnBean> productReturnBeanList = new ArrayList<>();

		for(RecommendProductBean tgt:tgtRecommendProductBeanList){
			boolean isRight = true;
			RecommendProductBean act = actRecommendProductBeanList.stream().filter(ele->ele.getProductId().equals(tgt.getProductId())).findAny().orElse(null);
			if (act==null){
				isRight = isRight && LoggerHelper.verifyEquals(true,false,apiName+", productId: "+tgt.getProductId()+"在期待结果中不存在",isWriteRet);
			}else{
				isRight = isRight && compareRecommendProductBean(tgt,act,apiName+ ", productId: "+ tgt.getProductId(),isWriteRet);
			}
			
			productReturnBeanList.add(new ProductReturnBean(tgt.getProductId(),isRight));
		}
			
		
		return productReturnBeanList;
	}
	
	
	public static boolean compareRecommendProductBean(RecommendProductBean tgtRecommendProductBean,RecommendProductBean actRecommendProductBean,String desc,boolean isWriteRet){
		Map<String, Object> tgtMap = MapUtil.pojoToMap(tgtRecommendProductBean);
		Map<String, Object> actMap = MapUtil.pojoToMap(actRecommendProductBean);
		return AssertServiceHelper.assertResultEqual(tgtMap,actMap,desc,isWriteRet);
	}
	
	//批量检查
	
	
	public static boolean compareProductDescExtraBean(ProductDescExtraBean tgtProductDescExtraBean,ProductDescExtraBean actProductDescExtraBean,String desc,boolean isWriteRet){
		boolean isRight=true;
		
		
		Map<String, Object> tgtMap = MapUtil.pojoToMap(tgtProductDescExtraBean);
		tgtMap.remove("DescPicList");
		tgtMap.remove("SizePicList");
		tgtMap.remove("NoticePicList");
		tgtMap.remove("SellerIntroPicList");
		tgtMap.remove("PropertyList");
		
		Map<String, Object> actMap = MapUtil.pojoToMap(actProductDescExtraBean);
		actMap.remove("DescPicList");
		actMap.remove("SizePicList");
		actMap.remove("NoticePicList");
		actMap.remove("SellerIntroPicList");
		actMap.remove("PropertyList");
		
		isRight = isRight && AssertServiceHelper.assertResultEqual(tgtMap,actMap,desc,isWriteRet);
		
		//list 数据比对
		if (tgtProductDescExtraBean.getDescPicList()!=null && tgtProductDescExtraBean.getDescPicList().size()!=0){
			for(int i=0;i<tgtProductDescExtraBean.getDescPicList().size();i++){
				if (tgtProductDescExtraBean.getDescPicList().get(i).contains("http://pc1")){
					tgtProductDescExtraBean.getDescPicList().set(i, tgtProductDescExtraBean.getDescPicList().get(i).replace("http://pc1", "http://pic1"));
				}
			}
			
			isRight = isRight && AssertServiceHelper.checkArray(tgtProductDescExtraBean.getDescPicList(), actProductDescExtraBean.getDescPicList(),desc+",DescPicList ",isWriteRet);
		}else{
			if (actProductDescExtraBean.getDescPicList()!=null){
				isRight = isRight && LoggerHelper.verifyEquals(0, actProductDescExtraBean.getDescPicList().size(), desc+",SizePicList ",isWriteRet);
			}
		}

		
		if (tgtProductDescExtraBean.getSizePicList()!=null && tgtProductDescExtraBean.getSizePicList().size()!=0){
			isRight = isRight && AssertServiceHelper.checkArray(tgtProductDescExtraBean.getSizePicList(), actProductDescExtraBean.getSizePicList(),desc+",SizePicList ",isWriteRet);
		}else{
			if (actProductDescExtraBean.getSizePicList()!=null){
				isRight = isRight && LoggerHelper.verifyEquals(0, actProductDescExtraBean.getSizePicList().size(), desc+",SizePicList ",isWriteRet);
			}
		}
		
		if (tgtProductDescExtraBean.getNoticePicList()!=null){
			isRight = isRight && AssertServiceHelper.checkArray(tgtProductDescExtraBean.getNoticePicList(), actProductDescExtraBean.getNoticePicList(),desc+",NoticePicList",isWriteRet);
		}else{
			isRight = isRight && LoggerHelper.verifyIsNull(actProductDescExtraBean.getNoticePicList(), desc+",NoticePicList",isWriteRet);
		}
		
		if (tgtProductDescExtraBean.getSellerIntroPicList()!=null){
			isRight = isRight && AssertServiceHelper.checkArray(tgtProductDescExtraBean.getSellerIntroPicList(), actProductDescExtraBean.getSellerIntroPicList(),desc+",SellerIntroPicList",isWriteRet);
		}else{
			isRight = isRight && LoggerHelper.verifyIsNull(actProductDescExtraBean.getSellerIntroPicList(), desc+",SellerIntroPicList",isWriteRet);
		}
		
		
		if (tgtProductDescExtraBean.getPropertyList()!=null){
			List<DescPropertyBean> tgtList = tgtProductDescExtraBean.getPropertyList();
			for(DescPropertyBean ele:tgtList){
				if (actProductDescExtraBean.getPropertyList()!=null){
					DescPropertyBean actBean = actProductDescExtraBean.getPropertyList().stream()
							.filter(y->y.getKey().equals(ele.getKey())).findFirst().orElse(null);
					if (actBean == null){
						
						isRight = isRight && LoggerHelper.verifyEquals(false,true, desc+",PropertyList Key"+ele.getKey()+"在期待值中不存在",isWriteRet);;
					}else{
						Map<String, Object> tgtPropMap = MapUtil.pojoToMap(ele);
						Map<String, Object> actPropMap = MapUtil.pojoToMap(actBean);
						isRight = isRight &&AssertServiceHelper.assertResultEqual(tgtPropMap,actPropMap,desc+",PropertyList",isWriteRet);
					}
				}else{
					isRight = isRight &&LoggerHelper.verifyEquals(false, true, desc+",PropertyList Key"+ele.getKey()+",实际节点为null",isWriteRet);
				}
			}
		
		}else{
			isRight = isRight && LoggerHelper.verifyIsNull(actProductDescExtraBean.getPropertyList(), desc+",PropertyList 应该为null",isWriteRet);
		}
	
		return isRight;
	}
	
	
	//批量检查
	public static List<ProductReturnBean> compareProductInListBean(List<String> productList, List<ProductInListBean> tgtProductInListBeanList,List<ProductInListBean> actProductInListBeanList,String apiName,boolean isWriteRet){
		
		List<ProductReturnBean> productReturnBeanList = new ArrayList<>();
		LoggerHelper.verifyEquals(tgtProductInListBeanList.size(), actProductInListBeanList.size(), apiName+",结果个数不一致",isWriteRet);
		for(String ele:productList){
			boolean isRight = true;
			ProductInListBean tgtProductInListBean = tgtProductInListBeanList.stream()
					.filter(tgt -> tgt.getProductId().equals(ele)).findAny().orElse(null);
			ProductInListBean actProductInListBean = actProductInListBeanList.stream()
					.filter(act -> act.getProductId().equals(ele)).findAny().orElse(null);
			if (tgtProductInListBean==null && actProductInListBean==null){
				//do nothing 新老接口都查不到
			}else{
				isRight = isRight &&LoggerHelper.verifyNotNull(tgtProductInListBean, apiName+",期待结果不该为null productId:" + ele,isWriteRet);
				isRight = isRight &&LoggerHelper.verifyNotNull(actProductInListBean, apiName+",实际结果不该为null productId:" + ele,isWriteRet);
				isRight = isRight &&compareProductInListBean(tgtProductInListBean, actProductInListBean, apiName+", productId: "+tgtProductInListBean.getProductId(),isWriteRet);
			}
			productReturnBeanList.add(new ProductReturnBean(ele,isRight));
		}

		
		return productReturnBeanList;
		
	}
	
	public static boolean compareProductInListBean(ProductInListBean tgtProductInListBean,ProductInListBean actProductInListBean,String desc,boolean isWriteRet){
		boolean isRight=true;
		
		Map<String, Object> tgtMap = MapUtil.pojoToMap(tgtProductInListBean);
		Map<String, Object> actMap = MapUtil.pojoToMap(actProductInListBean);
		

		if (Double.parseDouble(actProductInListBean.getVersion())>Double.parseDouble(tgtProductInListBean.getVersion())){
			tgtMap.remove("Version");
			actMap.remove("Version");
		}
		
		
		//如果isnew=true 比较新品时间
		if (tgtProductInListBean.isIsNewProduct()==true){
			//60秒的容错
			boolean startEqual = Math.abs(tgtProductInListBean.getNewStartTime().getTime() - actProductInListBean.getNewStartTime().getTime()) <= 60 * 1000;
			boolean endEqual = Math.abs(tgtProductInListBean.getNewEndTime().getTime() - actProductInListBean.getNewEndTime().getTime()) <= 60 * 1000;
			
			//如果有一个时间不对，差值超过60秒。和本地时间比较
			if (!startEqual || !endEqual){
				long newTime = YMTDateUtil.getDate().getTime();
				boolean startNearNow = Math.abs(actProductInListBean.getNewStartTime().getTime() - newTime) <= 5 * 60 * 1000;
				boolean endNearNow = Math.abs(actProductInListBean.getNewEndTime().getTime() - newTime) <= 5 * 60 * 1000;
				
				//只要有一个时间接近当前时间，就认为ok
				if(!startNearNow && !endNearNow){
					isRight = isRight && LoggerHelper.verifyEquals(true, false, desc + ", key:[NewStartTime],target value:[" + tgtProductInListBean.getNewStartTime() + "],actual value:[" + actProductInListBean.getNewStartTime()+"]" ,isWriteRet);
					isRight = isRight && LoggerHelper.verifyEquals(true, false, desc + ", key:[NewStartTime],target value:[" + tgtProductInListBean.getNewEndTime() + "],actual value:[" + actProductInListBean.getNewEndTime()+"]",isWriteRet);
				}
			}
		}
		
		
		ProductQueryCallService.removeIgnoreKey(tgtMap);
		ProductQueryCallService.removeIgnoreKey(actMap);
		
		ProductQueryCallService.handlerVaildStartAndEndTime(tgtProductInListBean.getProductId(),tgtProductInListBean.getSellerId(),tgtMap,actMap);
		isRight = isRight && AssertServiceHelper.assertResultEqual(tgtMap,actMap,desc,isWriteRet);
		
		return isRight;
		
	}
	
	
	//批量检查
	public static List<ProductReturnBean> compareTopProductInLiveBean(List<String> productList, List<TopProductInLiveBean> tgtTopProductInLiveBeanList,List<TopProductInLiveBean> actTopProductInLiveBeanList,String apiName,boolean isWriteRet){
		List<ProductReturnBean> productReturnBeanList = new ArrayList<>();
		LoggerHelper.verifyEquals(tgtTopProductInLiveBeanList.size(), actTopProductInLiveBeanList.size(), apiName+",结果个数不一致",isWriteRet);

		for(String ele:productList){
			boolean isRight=true;
			
			TopProductInLiveBean tgtTopProductInLiveBean = tgtTopProductInLiveBeanList.stream()
					.filter(tgt -> tgt.getProductId().equals(ele)).findAny().orElse(null);
			TopProductInLiveBean actTopProductInLiveBean = actTopProductInLiveBeanList.stream()
					.filter(act -> act.getProductId().equals(ele)).findAny().orElse(null);
			if (tgtTopProductInLiveBean==null && actTopProductInLiveBean==null){
				//do nothing 新老接口都查不到
			}else{
				if (tgtTopProductInLiveBean!=null&& actTopProductInLiveBean==null){
					isRight = isRight && LoggerHelper.verifyEquals(true, false, apiName+"期待结果不该为null productId:" + ele, isWriteRet);
				}else{
					isRight = isRight && compareTopProductInLiveBean(tgtTopProductInLiveBean, actTopProductInLiveBean, apiName +", productId: "+tgtTopProductInLiveBean.getProductId(), isWriteRet);
			
				}
			}
			productReturnBeanList.add(new ProductReturnBean(ele,isRight));
			
		}
		return productReturnBeanList;
	}
	
	public static boolean compareTopProductInLiveBean(TopProductInLiveBean tgtTopProductInLiveBean,TopProductInLiveBean actTopProductInLiveBean,String desc,boolean isWriteRet){
		Map<String, Object> tgtMap = MapUtil.pojoToMap(tgtTopProductInLiveBean);
		Map<String, Object> actMap = MapUtil.pojoToMap(actTopProductInLiveBean);
		return AssertServiceHelper.assertResultEqual(tgtMap,actMap,desc,isWriteRet);
	}
	
	
	//批量检查
	public static ProductReturnBean compareSecKillProductActivityStockBean(List<SecKillProductActivityStockBean> tgtSecKillProductActivityStockBeanList,List<SecKillProductActivityStockBean> actSecKillProductActivityStockBeanList,String apiName,boolean isWriteRet){
		
	
		boolean isRight=true;
		LoggerHelper.verifyEquals(tgtSecKillProductActivityStockBeanList.size(), actSecKillProductActivityStockBeanList.size(), apiName+",结果个数不一致",isWriteRet);
		for(SecKillProductActivityStockBean tgt:tgtSecKillProductActivityStockBeanList){
			
			SecKillProductActivityStockBean act = actSecKillProductActivityStockBeanList.stream().filter(ele->ele.getCatalogId().equals(tgt.getCatalogId())).findAny().orElse(null);
			
			if (tgt==null && act==null){
				//do nothing 新老接口都查不到
			}else{
				isRight = isRight && LoggerHelper.verifyNotNull(tgt, apiName+"期待结果不该为null ActivityId:" + tgt.getActivityId() + ", ProductId:"+tgt.getProductId()+", CatalogId:"+tgt.getCatalogId(),isWriteRet);
				isRight = isRight && LoggerHelper.verifyNotNull(act, apiName+"实际结果不该为null ActivityId:" + tgt.getActivityId() + ", ProductId:"+tgt.getProductId()+", CatalogId:"+tgt.getCatalogId(),isWriteRet);
				isRight = isRight && compareSecKillProductActivityStockBean(tgt, act, apiName+",CatalogId:"+tgt.getCatalogId(),isWriteRet);
			}
			
			
		}
		return new ProductReturnBean(tgtSecKillProductActivityStockBeanList.get(0).getProductId(),
				tgtSecKillProductActivityStockBeanList.get(0).getProductActivityId(),isRight);
		
	}
	
	public static boolean compareSecKillProductActivityStockBean(SecKillProductActivityStockBean tgtSecKillProductActivityStockBean,SecKillProductActivityStockBean actSecKillProductActivityStockBean,String desc,boolean isWriteRet){
		Map<String, Object> tgtMap = MapUtil.pojoToMap(tgtSecKillProductActivityStockBean);
		Map<String, Object> actMap = MapUtil.pojoToMap(actSecKillProductActivityStockBean);
		return AssertServiceHelper.assertResultEqual(tgtMap,actMap,desc,isWriteRet);
	} 
	
	
	
}
